const userDao = require("../dao/users");
const path = require("path");
module.exports = {
  findAll(req, res) {
    userDao.common("select * from t_student", [], function (err, data) {
      if (data) {
        console.log(data);
      }
    });
  },
  checkUserName(req, res) {
    userDao.common(
      "select * from users where username = ?",
      [req.query.username],
      function (err, data) {
        if (data) {
          if (data.length > 0) {
            res.send("1");
          } else {
            res.send("0");
          }
        }
      }
    );
  },
  register(req, res) {
    res.render("login", { title: "Express", username: `${req.body.username}` });
  },
  login(req, res) {
    userDao.common(
      "select * from users where username = ? and password = ?",
      [req.body.username, req.body.password],
      function (err, data) {
        console.log(data);
        if (data.length > 0) {
          res.cookie("uname", `${req.body.username}`, {
            httpOnly: false,
            signed: true,
            maxAge: 1000 * 60 * 30,
          });
          res.send("1");
        } else {
          res.send("0");
        }
      }
    );
  },
};
